package edu.jhu.secondhome.servlet;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.jhu.secondhome.database.accessors.HotelAccessor;
import edu.jhu.secondhome.database.accessors.RoomAccessor;
import edu.jhu.secondhome.database.beans.Hotel;
import edu.jhu.secondhome.database.beans.Room;

public class ReservationSearchServlet extends HttpServlet
{
  public void init() throws ServletException
  {

  }

  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
  {
    processRequest(req, resp);
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
  {
    processRequest(req, resp);
  }

  public void processRequest(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException
  {
    response.setContentType("text/html;charset=UTF-8");
    ServletContext servletContext = getServletContext();
    HttpSession session = request.getSession();

    ArrayList<String> errors = new ArrayList<String>();
    request.setAttribute("errors", errors);

    String checkin = request.getParameter( "check-in" );
    String checkout = request.getParameter( "check-out" );
    String numPeople = request.getParameter( "numPeople" );

    try
    {
      int occupancy = Integer.parseInt( numPeople );
      
      SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
      java.util.Date utilCheckinDate = format.parse( checkin );
      java.util.Date utilCheckoutDate = format.parse( checkout );
      
      long diff = utilCheckoutDate.getTime() - utilCheckinDate.getTime();
      long numDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);

      if( !( utilCheckinDate instanceof java.util.Date ) ||
          !( utilCheckoutDate instanceof java.util.Date ) )  
      {
        errors.add( "You must provide check-in and check-out dates in the form MM/dd/yyyy." );
        RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/index.jsp");
        dispatcher.forward(request, response);
        return;
      }

      Date checkinDate = new Date( utilCheckinDate.getTime() );

      HotelAccessor hotelDb = new HotelAccessor();
      List<Hotel> availableHotels = hotelDb.getAvailableHotelsForDateRangeAndOccupancy( checkinDate, (int) numDays, occupancy );
      request.setAttribute( "hotels", availableHotels );
      request.setAttribute( "check-in", checkin );
      request.setAttribute( "numDays", numDays );
      request.setAttribute( "numPeople", occupancy );

      RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/search/searchResults.jsp");
      dispatcher.forward(request, response);
      return;
    } catch ( SQLException e1 )
    {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      errors.add( e1.getMessage() );
    } catch ( ParseException pe )
    {
      errors.add( "You must supply valid check-in and check-out dates." );
    } catch ( NumberFormatException nfe )
    {
      errors.add( "You must supply a valid integer for number of people." );
    }

    RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
    return;
  }
}
